package my.state.headfirst.good;

/**
 * 幸运者的 可销售状态
 */
public class WinnerState implements State {

    //transient 修饰是 proxy 模式中加入的改进
    transient private GunballMachine gunballMachine;

    public WinnerState(GunballMachine gunballMachine){
        this.gunballMachine = gunballMachine;
    }

    @Override
    public void insertQuarter() {
        System.out.println("请先出糖果吧！");
    }

    @Override
    public void ejectQuarter() {
        System.out.println("亲，已经出过糖果了，不能退币呀！");
    }

    @Override
    public void turnCrank() {
        System.out.println("亲，别想骗过糖果机出两次糖果");
    }

    @Override
    public void dispense() {
        gunballMachine.releaseBall();
        if(gunballMachine.getCount()==0){
            gunballMachine.setState(gunballMachine.getSoldOutState());
        }else{
            System.out.println("您是幸运者，将会出两颗糖果！");
            gunballMachine.releaseBall();
            if(gunballMachine.getCount()>0){
                gunballMachine.setState(gunballMachine.getNoQuarterState());
            }else{
                gunballMachine.setState(gunballMachine.getSoldOutState());
            }
        }
    }
}
